package com.qianfeng.lingshijia;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.mob.tools.utils.UIHandler;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;

/**
 * Created by Administrator on 2016/1/14.
 */
public class PersonLoginActivity extends Activity implements Handler.Callback, View.OnClickListener, PlatformActionListener{

    private static final int MSG_USERID_FOUND = 1;
    private static final int MSG_LOGIN = 2;
    private static final int MSG_AUTH_CANCEL = 3;
    private static final int MSG_AUTH_ERROR = 4;
    private static final int MSG_AUTH_COMPLETE = 5;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);

        setContentView(R.layout.person_login);
        findViewById(R.id.tv_qq).setOnClickListener(this);
        findViewById(R.id.tv_wechat).setOnClickListener(this);

    }

    @Override
    protected void onDestroy() {
        ShareSDK.stopSDK(this);
        super.onDestroy();
    }


    @Override
    public void onClick(View v) {
            switch (v.getId()){
                case R.id.tv_qq:
                    Platform qzon=ShareSDK.getPlatform(this, QZone.NAME);
                    qzon.setPlatformActionListener(this);
                    qzon.authorize();
                    break;
                case R.id.tv_wechat:
                {
                    Wechat wechat=new Wechat(this);
                    authorize(wechat);
                }
                    break;
            }
    }

    private void authorize(Platform plat) {
        //在授权之后才有效果
        if(plat.isValid()){
            String userId=plat.getDb().getUserId();
            if(!TextUtils.isEmpty(userId)){
                UIHandler.sendEmptyMessage(MSG_USERID_FOUND, this);
                login(plat.getName(),userId,null);
                // 自己实现登录

                PlatformDb platDB = plat.getDb();//获取数平台数据DB
                //通过DB获取各种数据
                platDB.getToken();
                platDB.getUserGender();
                platDB.getUserIcon();
                platDB.getUserId();
                platDB.getUserName();

                return;
            }
        }
        plat.setPlatformActionListener(this);
        plat.SSOSetting(true);
        plat.showUser(null);
    }

    private void login(String plat, String userId, HashMap<String,Object> userInfo) {
        Message msg=new Message();
        msg.what=MSG_LOGIN;
        msg.obj=plat;
        UIHandler.sendMessage(msg, this);
    }

    //授权成功
    @Override
    public void onComplete(Platform platform, int action, HashMap<String, Object> res) {
        if(action == Platform.ACTION_USER_INFOR){
            UIHandler.sendEmptyMessage(MSG_AUTH_COMPLETE,this);
            login(platform.getName(),platform.getDb().getUserId(),res);

        }
        if(action ==Platform.ACTION_USER_INFOR){
            PlatformDb platDB=platform.getDb(); //获取平台数据
            platDB.getToken();
            platDB.getUserGender();
            platDB.getUserIcon();
            platDB.getUserId();
            platDB.getUserName();
        }
    }

    @Override
    public void onError(Platform platform, int action, Throwable t) {
        if(action == Platform.ACTION_USER_INFOR){
            UIHandler.sendEmptyMessage(MSG_AUTH_ERROR, this);
        }
        t.printStackTrace();
    }

    @Override
    public void onCancel(Platform platform, int action) {
        if(action == Platform.ACTION_USER_INFOR){
            UIHandler.sendEmptyMessage(MSG_AUTH_CANCEL,this);
        }
    }

    public boolean handleMessage(Message msg)
    {
        switch (msg.what)
        {
            case MSG_USERID_FOUND:
            {
                Toast.makeText(this, R.string.userid_found, Toast.LENGTH_SHORT).show();
            }
            break;
            case MSG_LOGIN:
            {

                String text = getString(R.string.logining, msg.obj);
                Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
                System.out.println("---------------");
            }
            break;
            case MSG_AUTH_CANCEL:
            {
                Toast.makeText(this, R.string.auth_cancel, Toast.LENGTH_SHORT).show();
                System.out.println("-------MSG_AUTH_CANCEL--------");
            }
            break;
            case MSG_AUTH_ERROR:
            {
                Toast.makeText(this, R.string.auth_error, Toast.LENGTH_SHORT).show();
                System.out.println("-------MSG_AUTH_ERROR--------");
            }
            break;
            case MSG_AUTH_COMPLETE:
            {
                Toast.makeText(this, R.string.auth_complete, Toast.LENGTH_SHORT).show();
                System.out.println("--------MSG_AUTH_COMPLETE-------");
            }
            break;
        }
        return false;
    }
}
